#define   E_APP
#include "H/APP.H"
#include <stdio.h>

//自设阈值适用于PT_CP_24009_V1.X PCB,其他PCB请自行修改THS1~THS20阈值,阈值越小越灵敏
//自适应阈值适用于PT_CP_24009_V1.X PCB，其他PCB请自行修改ATRL与ATRH阈值,阈值越小越灵敏
//PT_CP_24009_V1.X PCB CMOD=20nF
#define   MODE       1   //=1 阈值自适应模式       =0 阈值自设模式 
#define   LED_EN     1   //=1 触摸通道LED使能      =0 触摸通道LED不使能

uint32_t Led_Running = 0;
bit  F_CPL = 0;

/*****************************************************
函数名称：void PT2020_TouchKey_Init()
函数功能：配置PT2020
入口参数：void
出口参数：void
*****************************************************/
uchar code C_TABLE[] ={0xFF,0xFF,0x0F,\
                       0x51,0X43,0X38,0X30,0X32,0X5F,0X4C,0X3E,\
                       0X34,0X33,0X5B,0X48,0X3A,0X2F,0X2A,0X6A,\
                       0X47,0X37,0X2C,0X25};
uchar code C_TABLE_LED[] ={0xFF,0xFF,0x0F,\
                       0X25,0X1F,0X1D,0X15,0X15,0X26,0X24,0X1D,\
                       0X1A,0X1B,0X24,0X22,0X1C,0X19,0X17,0X2A,\
                       0X1E,0X1D,0X18,0X17};
                       
void PT2020_TouchKey_Init()
{
//    //设置1~16触摸通道及LED使能  
//    unsigned int R_RE;
//reset: 

//    #if  MODE
////      自适应阈值  
//        if(PT2020_I2C_WriteData(PT2020_THEN1,0xFF)==M_FAIL)   goto reset; //写0xFF到THEN1寄存器，若写错误则reset重新写
//        if(PT2020_I2C_WriteData(PT2020_THEN2,0xFF)==M_FAIL)   goto reset; //写0xFF到THEN2寄存器，若写错误则reset重新写  
//        if(PT2020_I2C_WriteData(PT2020_THEN3,0x0F)==M_FAIL)   goto reset;   
//        if(PT2020_I2C_WriteData(PT2020_ATRL,145)==M_FAIL)     goto reset;  
//        if(PT2020_I2C_WriteData(PT2020_ATRH,0x00)==M_FAIL)    goto reset; 
////        if(PT2020_I2C_WriteData(PT2020_LPST,20)==M_FAIL) goto reset; 
//  
//        #if  LED_EN
//        if(PT2020_I2C_WriteData(PT2020_LEDEN1,0xFF)==M_FAIL)   goto reset; 
//        if(PT2020_I2C_WriteData(PT2020_LEDEN2,0xFF)==M_FAIL)   goto reset;  
//        if(PT2020_I2C_WriteData(PT2020_LEDEN3,0x0F)==M_FAIL)   goto reset;  
////      自适应阈值模式，超长按键复位使能，LED复用使能
//        if(PT2020_I2C_WriteData(PT2020_CTRL1,0xD1)==M_FAIL)    goto reset;
//        #else
////      自适应阈值模式，超长按键复位使能，LED复用不使能
//        if(PT2020_I2C_WriteData(PT2020_CTRL1,0xC1)==M_FAIL)    goto reset;
//        #endif
//    #else
////      自设阈值
//        #if  LED_EN
////          自设阈值,根据实际情况自行设置C_TABLE_LED表
//            if(PT2020_I2C_ContWriteData(PT2020_THEN1,&C_TABLE_LED,23)==M_FAIL) goto reset;//向PT2020写THEN1~THS20的连续23个数据，若写错误则reset重新写        
//            if(PT2020_I2C_WriteData(PT2020_LEDEN1,0xFF)==M_FAIL) goto reset;
//            if(PT2020_I2C_WriteData(PT2020_LEDEN2,0xFF)==M_FAIL) goto reset;
//            if(PT2020_I2C_WriteData(PT2020_LEDEN3,0x0F)==M_FAIL) goto reset;
////          自设阈值模式，超长按键复位使能，LED复用使能
//            if(PT2020_I2C_WriteData(PT2020_CTRL1,0x51)==M_FAIL) goto reset;
//        #else
////          自设阈值,根据实际情况自行设置C_TABLE表
//            if(PT2020_I2C_ContWriteData(PT2020_THEN1,&C_TABLE,23)==M_FAIL) goto reset;//向PT2020写THEN1~THS20的连续23个数据，若写错误则reset重新写        
////          自设阈值模式，超长按键复位使能，LED复用不使能
//            if(PT2020_I2C_WriteData(PT2020_CTRL1,0x41)==M_FAIL) goto reset;
//        #endif
//    #endif

////以下是数据校验:读取配置寄存器，若与写入值不一致则reset重新配置
//    #if  MODE
////      自适应阈值    
//        R_RE = PT2020_I2C_ReadData(PT2020_THEN1);  if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xFF)) goto reset; //读失败或读数据错误返回reset重新写
//        R_RE = PT2020_I2C_ReadData(PT2020_THEN2);  if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xFF)) goto reset; //读失败或读数据错误返回reset重新写
//        R_RE = PT2020_I2C_ReadData(PT2020_THEN3);  if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0x0F)) goto reset; //读失败或读数据错误返回reset重新写   
//        R_RE = PT2020_I2C_ReadData(PT2020_ATRL);   if((R_RE&RD_FAIL)||((R_RE&0xFF)!=145))  goto reset; //读失败或读数据错误返回reset重新写
//        R_RE = PT2020_I2C_ReadData(PT2020_ATRH);   if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0x00)) goto reset; //读失败或读数据错误返回reset重新写 
////        R_RE = PT2020_I2C_ReadData(PT2020_LPST);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=20)) goto reset;   //读失败或读数据错误返回reset重新写        
//        #if  LED_EN
//            R_RE = PT2020_I2C_ReadData(PT2020_LEDEN1);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xFF)) goto reset; 
//            R_RE = PT2020_I2C_ReadData(PT2020_LEDEN2);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xFF)) goto reset; 
//            R_RE = PT2020_I2C_ReadData(PT2020_LEDEN3);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0x0F)) goto reset; 
////          自适应阈值模式，超长按键复位使能，LED复用使能
//            R_RE = PT2020_I2C_ReadData(PT2020_CTRL1);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xD1))  goto reset; 
//        #else
////          自适应阈值模式，超长按键复位使能，LED复用不使能
//            R_RE = PT2020_I2C_ReadData(PT2020_CTRL1);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xC1))  goto reset; 
//        #endif
//    #else
////      自设阈值
//        #if  LED_EN
//            PT2020_I2C_ContReadData(PT2020_THEN1,&R_RECEIVE,23);   //向PT2020读THEN1~THS20的连续23个数据
//            if(COMPARE(&R_RECEIVE,&C_TABLE_LED,23)==FAIL) goto reset;  //读到的数据比对失败则复位        
//            R_RE = PT2020_I2C_ReadData(PT2020_LEDEN1);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xFF)) goto reset;
//            R_RE = PT2020_I2C_ReadData(PT2020_LEDEN2);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0xFF)) goto reset;
//            R_RE = PT2020_I2C_ReadData(PT2020_LEDEN3);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0x0F)) goto reset;
////          自设阈值模式，超长按键复位使能，LED复用使能
//            R_RE = PT2020_I2C_ReadData(PT2020_CTRL1);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0x51))  goto reset;
//        #else
//            PT2020_I2C_ContReadData(PT2020_THEN1,&R_RECEIVE,23);   //向PT2020读THEN1~THS20的连续23个数据
//            if(COMPARE(&R_RECEIVE,&C_TABLE,23)==FAIL) goto reset;  //读到的数据比对失败则复位           
////          自设阈值模式，超长按键复位使能，LED复用不使能
//            R_RE = PT2020_I2C_ReadData(PT2020_CTRL1);if((R_RE&RD_FAIL)||((R_RE&0xFF)!=0x41))  goto reset;
//        #endif
//    #endif
}

/*****************************************************
函数名称：void PT2020_SET_UART0()
函数功能：PT2020配置信息，UART0打印
入口参数：void
出口参数：void
*****************************************************/
void PT2020_SET_UART0()
{
#if  LED_EN  
uchar code *P= &C_TABLE_LED[3];
#else
uchar code *P= &C_TABLE[3];  
#endif  
  
    #if  MODE  
    //自适应阈值  
        R_SET[0]=PT2020_THEN1;
        R_SET[1]=0xFF;

        R_SET[2]=PT2020_THEN2;
        R_SET[3]=0xFF; 
                        //延时是为了让串口自动换行   
        R_SET[4]=PT2020_THEN3;
        R_SET[5]=0x0F;    
        UART0_MULTIBYTE_SEND(R_SET,6);              //串口数据发送
        delay_ms(200);

        R_SET[0]=PT2020_ATRH;
        R_SET[1]=0;
        R_SET[2]=PT2020_ATRL;
        R_SET[3]=145;  
//        R_SET[4]=PT2020_LPST;
//        R_SET[5]=20;      
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200); 
  
        #if  LED_EN
        R_SET[0]=PT2020_LEDC1;
        R_SET[1]=0xFF;
        R_SET[2]=PT2020_LEDC2;
        R_SET[3]=0xFF;  
        R_SET[4]=PT2020_LEDC3;
        R_SET[5]=0x0F; 
        UART0_MULTIBYTE_SEND(R_SET,6);              //串口数据发送
        delay_ms(200);
        R_SET[0]=PT2020_CTRL1;
        R_SET[1]=0xD1; 
        UART0_MULTIBYTE_SEND(R_SET,2);              //串口数据发送        
        #else
        R_SET[0]=PT2020_CTRL1;
        R_SET[1]=0xC1; 
        UART0_MULTIBYTE_SEND(R_SET,2);              //串口数据发送
        #endif
     #else
     //自设阈值
        R_SET[0]=PT2020_THEN1;
        R_SET[1]=0xFF;
        R_SET[2]=PT2020_THEN2;
        R_SET[3]=0xFF;
        R_SET[4]=PT2020_THEN3;
        R_SET[5]=0x0F;    
        UART0_MULTIBYTE_SEND(R_SET,6);              //串口数据发送
        delay_ms(200); 
  
        R_SET[0]=PT2020_THS1;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS2;
        R_SET[3]=*P++;   
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);     
        R_SET[0]=PT2020_THS3;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS4;
        R_SET[3]=*P++;   
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);  
        R_SET[0]=PT2020_THS5;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS6;
        R_SET[3]=*P++;    
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);         
        R_SET[0]=PT2020_THS7;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS8;
        R_SET[3]=*P++;    
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);         
        R_SET[0]=PT2020_THS9;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS10;
        R_SET[3]=*P++;    
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);         
        R_SET[0]=PT2020_THS11;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS12;
        R_SET[3]=*P++;   
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);         
        R_SET[0]=PT2020_THS13;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS14;
        R_SET[3]=*P++;    
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);         
        R_SET[0]=PT2020_THS15;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS16;
        R_SET[3]=*P++;   
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);   
        R_SET[0]=PT2020_THS17;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS18;
        R_SET[3]=*P++;    
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);          
        R_SET[0]=PT2020_THS19;
        R_SET[1]=*P++;
        R_SET[2]=PT2020_THS20;
        R_SET[3]=*P++;   
        UART0_MULTIBYTE_SEND(R_SET,4);              //串口数据发送
        delay_ms(200);          
        #if  LED_EN
            R_SET[0]=PT2020_LEDEN1;
            R_SET[1]=0xFF;
            R_SET[2]=PT2020_LEDEN2;
            R_SET[3]=0xFF;    
            R_SET[4]=PT2020_LEDEN3;
            R_SET[5]=0x0F; 
            UART0_MULTIBYTE_SEND(R_SET,6);              //串口数据发送
            delay_ms(200); 
            R_SET[0]=PT2020_CTRL1;
            R_SET[1]=0x51;    
        #else
            R_SET[0]=PT2020_CTRL1;
            R_SET[1]=0x41;           
        #endif
        UART0_MULTIBYTE_SEND(R_SET,2);              //串口数据发送            
        delay_ms(200);        
    #endif   
}

/*****************************************************
函数名称：unsigned char PT2020_UartReadTouchKeyCh()
函数功能：读键值，并由串口(P31)打印
入口参数：void
出口参数：void
******************************************************/
//#define   RD_FAIL          0X100  读失败
unsigned char PT2020_UartReadTouchKeyCh()
{
    unsigned char Buffer[3];         //键值数组
    unsigned int  R_READ_DATA;
READ_CTRL1_BIT0:
    R_READ_DATA = PT2020_I2C_ReadData(PT2020_CTRL1);    //读配置标志
    if((R_READ_DATA&RD_FAIL)!=0)  goto READ_CTRL1_BIT0; //读失败
    else if((R_READ_DATA&0x01)==0) {                    //判断配置标志是否有效（必须判断，防止PT2020芯片异常复位丢失配置）
    PT2020_TouchKey_Init(); return 0; }                 //初始化修改配置;

    R_READ_DATA=PT2020_I2C_ReadData(PT2020_KEYH);       //读通道17-20的u8_data
    if((R_READ_DATA&RD_FAIL)!=0)  goto READ_CTRL1_BIT0; //读失败
    else  Buffer[0]=(unsigned char)R_READ_DATA;

    R_READ_DATA=PT2020_I2C_ReadData(PT2020_KEYM);       //读通道9-16的u8_data
    if((R_READ_DATA&RD_FAIL)!=0)  goto READ_CTRL1_BIT0; //读失败
    else  Buffer[1]=(unsigned char)R_READ_DATA;

    R_READ_DATA=PT2020_I2C_ReadData(PT2020_KEYL);       //读通道1-8的u8_data
    if((R_READ_DATA&RD_FAIL)!=0)  goto READ_CTRL1_BIT0; //读失败
    else  Buffer[2]=(unsigned char)R_READ_DATA;


    if((Buffer[0]!=0)||(Buffer[1]!=0)||(Buffer[2]!=0)){P02=1;}   else {P02=0;}   //指示灯输出
    UART0_MULTIBYTE_SEND(Buffer,3);          //串口数据发送
    return 0;
}


/*****************************************************
*函数名称：void SYS_INIT()
*函数功能：系统初始化
*入口参数：void
*出口参数：void
*****************************************************/
void SYS_INIT()
{
    uint8_t temp_test = 0;
		GPIO_Init();            //IO初始化
    UART0_Init(115200);     //UART0 115200bps
    WDT_CLOSE();            //关闭看门狗，有需要自行开启
    INT0_INIT(0,0x05);      //INT0下降沿触发，INT0从P05输入                             //INT0产生中断进行I2C通讯获取PT2020键值 
		TIMER0_TIMER_INIT(1000);	 
		IPL = 0x01;
		timereset1 = 2000;
    timereset2 = 2000;
		EA = 1;
}

/*****************************************************
*函数名称：void COMPARE()
*函数功能：比较函数
*入口参数：void
*出口参数：返回0比较失败，返回1比较成功
*****************************************************/
bit COMPARE(unsigned int *P1,unsigned char code *P2,unsigned char R_L)
{
    while(R_L)
    {
        if(*P1!=(uint) *P2) return FAIL;
        else
        {
            R_L--;
            P1++;
            P2++;
        }
    }
    return TRUE;
}

void Flowing_Led_Process(void)
{
		uint8_t err_t = 0;
		uint8_t ReadBack = 0xFF;
		static uint8_t flash_ui_offset = 0;
		switch(flash_ui_offset)
		{
			case 0:
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC1,0x10);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC2,0x11);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC3,0x01);
					flash_ui_offset++;
					break;
			case 1:
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC1,0x20);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC2,0x22);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC3,0x02);
					flash_ui_offset++;
					break;
			case 2:
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC1,0x40);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC2,0x44);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC3,0x04);
					flash_ui_offset++;
					break;
			case 3:
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC1,0x80);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC2,0x88);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC3,0x08);
					flash_ui_offset++;
					break;
			case 4:
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC1,0xF0);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC2,0xFF);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC3,0x0F);
					flash_ui_offset++;
					break;
			case 5:
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC1,0x00);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC2,0x00);
					PT2020_Touch_Set_ReuseLed_Output_config(PT2020_LEDC3,0x00);
					flash_ui_offset++;
					break;
			default:
					F_StartUp_FlashUI = 0;
				err_t = PT2020_Touch_CTRL2_Enable_Config(0x00);				//立即进入低功耗关闭，低功耗状态LDO关闭,LED单独控制关闭
				if(err_t == -1)
				{
#ifdef	DEBUG
						printf("CTRL2 config failed!\r\n");
#endif
				}
				err_t = PT2020_Touch_CTRL1_Enable_Config(TOUCH_CTRL1_SET);				//任何配置发生了更改，都必须将[CTRL1,0]写1才能生效。默认复用的LED在触摸按下时,LED亮，松开LED熄灭,跟随模式
				if(err_t == -1)
				{
#ifdef	DEBUG
						printf("CTRL1 config failed!\r\n");
#endif
				}		
				delay_ms(1);	
				ReadBack = PT2020_I2C_ReadData(PT2020_CTRL2);
				if(ReadBack != 0)
				{
#ifdef	DEBUG
				printf(" CTRL2 is Read/write data mismatch!\r\n");
#endif	
				}
				ReadBack = PT2020_I2C_ReadData(PT2020_CTRL1);
				if(ReadBack != TOUCH_CTRL1_SET)
				{
#ifdef	DEBUG
				printf(" CTRL1 is Read/write data mismatch!\r\n");
#endif	
				}
					break;
		}
}



